<?
include("common/header.html");
include_once("class/ProductInfo.php");
include_once("class/Member.php");
$ac=$GLOBALS['_ac'];
$pw=$GLOBALS['_pw'];
$s = new SoapClient(null,array("location" => "http://localhost/open/openService/pft_insideMX.php","uri" => "www.16u.com?ac_16u=$ac|pw_16u=$pw|auth_16u=true"));
$memberID=$_SESSION['sid'];
$m=new Member($s,$memberID);
$pid=abs($_REQUEST['pid']);
$aid=abs($_REQUEST['aid']);
$p=new ProductInfo($s,$pid,$aid);
$ptypes=ProductInfo::$ptype;
$p_info=$p->pInfo();//返回产品信息
$p->saleCheck();//查看产品是否在售
$begintime=$p->getMinValidTime($begintime);/*产品有效起始日期*/
if($begintime==-1){
	getOut("home.html","无有效出行日期，请更换产品");
}
if(!$p->supply($memberID)){//查看产品是否供应供应给该分销商
	getOut("home.html","无购买权限");
}	//var_dump($p_info);
//分销商id
// $jsprice=$p->jsPrice($memberID,$begintime);//返回产品分销价
// $uprice=$p->uPrice($memberID,$begintime);//返回产品零售价

$jsprice=$p->jsPrice_n($begintime);//返回产品分销价
$uprice=$p->uPrice_n($begintime);//返回产品零售价

$buynum=$p_info['buy_limit_low']?$p_info['buy_limit_low']:1;
//附属产品
$products=array();
$products[]=array(
    "pid"=>$pid,
    "id"        =>"purchaseNum",
    "name"      =>"tnum",
    "ttitle"    =>$p_info['ttitle'].($p_info['v_time_limit']==0 ? '' : '(<span style="color:#F33">验票时间'.str_replace('|', '至', $p_info['v_time_limit']).'</span>)'),
    "buynum"    =>$buynum,
    "jsprice"   =>$jsprice,
    "uprice"    =>$uprice,
    "tprice"    =>$p_info['tprice'],
    "minTime"   =>$begintime,
    "ddays"     =>$p_info['ddays'],
    "buy_limit_low" =>$p_info['buy_limit_low'],
    'buy_limit_up'  =>$p_info['buy_limit_up'],
    'pay'       =>$p_info['pay'],
    'sendVoucher'=>$p_info['sendVoucher'],
	'order_start'=>$p_info['order_start'],
	'order_end'=>$p_info['order_end'],
);
$payMethod =  $p_info['pay'];
if ( $p_info['p_type']!="F" && $p_info['v_time_limit']==0 && $p_info['sync_status']!=2) {
	$c_pros=$m->salePros($p_info['lid']);//查询会员可分销产品信息
	foreach($c_pros as $row){
		if($row['sapply_did']!=$_REQUEST['aid'] && $row['apply_did']!=$_SESSION['memberID']) {
            continue;
        }
		$c_pids[]=$row['pid'];
	}
    if (count($c_pids)) {
        foreach($c_pids as $c_pid){
            if($c_pid==$p_info['pid']) continue;
            $apro=new ProductInfo($s,$c_pid,$aid);
            try{
                // $c_jsprice=$apro->jsPrice($memberID,$begintime);
                // $c_uprice=$apro->uPrice($memberID,$begintime);
                
                $c_jsprice=$apro->jsPrice_n($begintime);
                if($c_jsprice==-1 || is_array($c_jsprice)) continue;
                $c_uprice=$apro->uPrice_n($begintime);
                if($c_uprice==-1) continue;
                $c_pro=$apro->pInfo();
                
                if($c_pro['v_time_limit'] != 0 || $c_pro['sync_status']==2) continue;//验票时间是否不限
                if($c_pro['pay'] !=$payMethod) continue;
                // 
                if($products[0]['sendVoucher']!=$c_pro['sendVoucher'])continue;
                $time=strtotime($begintime)+($c_pro['ddays']-$p_info['ddays'])*24*3600;
                $minTime=date("Y-m-d",$time);
                $products[]=array("pid"=>$c_pro['pid'],"id"=>$c_pro['id'],"name"=>"c_pids[".$c_pid."]","ttitle"=>$c_pro['ttitle'],"buynum"=>0,"jsprice"=>$c_jsprice,"uprice"=>$c_uprice,"tprice"=>$c_pro['tprice'],"minTime"=>$minTime,"buy_limit_low"=>$c_pro['buy_limit_low'],'buy_limit_up'=>$c_pro['buy_limit_up'],'valid'=>_htmlValid($c_pro),'order_start'=>$c_pro['order_start'],'order_end'=>$c_pro['order_end']);
            }catch(Exception $e){
                //结算价未设置
            }
        }
    }

}
//供应商信息
$str="select dname,qq,mobile,cname from pft_member where id=".$p_info['sapply_did']." limit 1";
$GLOBALS['le']->query($str);
$a_info=$GLOBALS['le']->fetch_assoc();
// 产品有效期
$valid = _htmlValid($p_info);
$products[0]['valid'] = $valid;

//产品说明
$timeTitle=($p_info['ddays']==0)?"有效期内，可预订当天门票":"有效期内，需提前".($p_info['ddays'])."天预订";
$unitT=$p_info['p_type']=="C"?"间":"张";
$tips=$p_info['ddays']>0?"需提前".$p_info['ddays']."天预订，":"可预订当日票，";
$tips.=$p_info['buy_limit_low']>0?$p_info['buy_limit_low'].$unitT."起订，":"1".$unitT."起订，";
if($p_info['p_type']=="A"){
	$tips.=($p_info['delaydays']>0)?"游玩日期起".$p_info['delaydays']."天内有效，":"游玩日期当日有效，";
}
$tips.=($p_info['status']!=0)?"<i>".($p_info['mdays']!=0?"自动":"人工")."确认</i>后生效，":"";
$tips=$tips.$p_info['notes'];
//金额
$v['tmoney']=$m->tmoney($p_info['sapply_did']);//查询会员账户可用授信额度
$v['money']=$m->money();//查询会员账户余额
$cinfos=$m->cinfos(-1);//查询常用联系人
if($v['tmoney']>$jsprice*$tnum){
	$paymodeCheck0="";
	$paymodeCheck1="";
	$paymodeCheck2="checked";
}elseif($v['money']>$jsprice*$tnum){
	$paymodeCheck0="checked";
	$paymodeCheck1="";
	$paymodeCheck2="";
}else{
	//$paymodeCheck0="checked";//暂时关闭支付宝
	$paymodeCheck0="";
	$paymodeCheck1="checked";
	$paymodeCheck2="";
}
function getOut($url,$msg=null){
	$js="<script>";
	if($msg){ $js.="alert('$msg');" ;}
	$js.="window.location='$url'</script>";
	exit($js);
}
$token = '6f0c6c49daa58b9d802189ac932dcd65';
$appid = base64_encode('www.12301.cc');
if($_SESSION['sid']==1433){
	print_r($products);
}
function _htmlValid($p_info){
	if($p_info['delaydays']>0){
		if($p_info['delaytype']==0) $valid = '游玩日期(含)'.$p_info['delaydays'].'天后有效';
		if($p_info['delaytype']==1) $valid = '下单日期(含)'.$p_info['delaydays'].'天后有效';
	}
	if($p_info['order_start']!=''){
		$valid = '有效日期从'.$p_info['order_start'].' 至 '.$p_info['order_end'];
	}
	if($p_info['order_start']=='' && $p_info['delaydays'] == 0){
		$valid = '订单当天有效';
	}
	return $valid;
}

if($p_info['p_type']!='C'){
    $land_f_info = $p_info;
}
include("tpl/productOrder_dt_dt.tpl");
include("common/footer.html");
?>